﻿using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.Lor;
using ExpenseSystem.Common;
using System.Collections.Generic;
using System.Web;
using System;

namespace ExpenseSystem.Fether
{
    public abstract class SystemFetcher<T>  where T:SystemModel,new()
    {
        int rows;
        protected string tran = HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString();

        public SystemFetcher()
        {
        }
        public virtual T Get(long id, out string message)
        {
            Restrain restrainId = Restrain.Eq("AutoId", id).And(Restrain.Eq("Avaliable",true));
            message = null;
            IList<T> result = SqlProvider.GetDbInstance<T>().Select(tran,OrderBy.GetDefault() , Limit.GetDefault(), out message, out rows, restrainId);
            if (result.Count > 0)
                return result[0];
            return null;
        }
        public virtual IList<T> List( IList<OrderBy> orderBy, Limit limit, out string message, out int rows, params Restrain[] restrains)
        {
            IList<Restrain> restrainList = new List<Restrain>(restrains);
            restrainList.Add(Restrain.Eq("Avaliable",true));
            message = null;
            Restrain[] param = new Restrain[restrainList.Count];
            restrainList.CopyTo(param, 0);
            IList<T> result = SqlProvider.GetDbInstance<T>().Select(tran, orderBy, limit, out message, out rows, param);
            return result;
        }
    }
}
